package com.bibrApp.difu.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.R;

import androidx.appcompat.app.AppCompatDialog;


/**
 * Created by dj on 2020/4/8
 */

public class InputPinDialog extends AppCompatDialog {

    public InputPinDialog(Context context, int theme, View view) {
        super(context, theme);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        int[] sd = ScreenUtils.getScreenDispaly(context);
        params.width = (int) (sd[0] * 0.9);
        params.width = 1000;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        setContentView(view);
    }

    public static class Builder {
        private Context context;
        private int theme;
        private InputPinDialog dialog;
        private OnSureBtnClick onSureBtnClick;

        public Builder(Context context, int theme) {
            this.context = context;
            this.theme = theme;
        }


        public InputPinDialog creater() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_pin_u, null);
            dialog = new InputPinDialog(context, theme, view);
            dialog.setCanceledOnTouchOutside(false);//点击其他区域不可关闭弹窗
            dialog.setCancelable(false);
            StringBuilder sb =new StringBuilder();
            ImageView iv1 = view.findViewById(R.id.num_1);
            ImageView iv2 = view.findViewById(R.id.num_2);
            ImageView iv3 = view.findViewById(R.id.num_3);
            ImageView iv4 = view.findViewById(R.id.num_4);
            ImageView iv5 = view.findViewById(R.id.num_5);
            ImageView iv6 = view.findViewById(R.id.num_6);
            ImageView iv7 = view.findViewById(R.id.num_7);
            ImageView iv8 = view.findViewById(R.id.num_8);
            ImageView iv9 = view.findViewById(R.id.num_9);

            TextView tvPwd = view.findViewById(R.id.dialog_password_tv);
            ImageView delIV = view.findViewById(R.id.dialog_del);
            Button sureBtn = view.findViewById(R.id.dialog_btn_sure);
            delIV.setOnClickListener(v ->{
                if(sb.toString().length()>0){
                    sb.deleteCharAt(sb.length()-1);
                }
                if(sb.toString().length() == 0){
                    sureBtn.setEnabled(false);
                    sureBtn.setClickable(false);
                    tvPwd.setText("");
                }else{
                    tvPwd.setText(sb.toString());
                }

            });



            iv1.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(1);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv2.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(2);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv3.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(3);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv4.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(4);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv5.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(5);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv6.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(6);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv7.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(7);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv8.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(8);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            iv9.setOnClickListener(v -> {
                if(sb.toString().length()<9){
                    sb.append(9);
                    tvPwd.setText(sb.toString());
                }
                sureBtn.setEnabled(true);
                sureBtn.setClickable(true);
            });
            sureBtn.setOnClickListener(v->{
                if(onSureBtnClick != null){
                    onSureBtnClick.onClickSure(sb.toString());
                }
            });
            return dialog;
        }




        public void dismiss() {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }

        public InputPinDialog show() {
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
            return dialog;
        }
        public OnSureBtnClick getOnSureBtnClick() {
            return onSureBtnClick;
        }

        public void setOnSureBtnClick(OnSureBtnClick onSureBtnClick) {
            this.onSureBtnClick = onSureBtnClick;
        }

    }



    public interface OnSureBtnClick {
        void onClickSure(String numTV);
    }
}
